package 线程2;
//1.简单粗暴的办法,就是使用一个boolean,来作为结束循环标记
public class TestDemo2 {
    private static boolean flag = true;
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(){
            @Override
            public void run() {
                while(flag){
                    System.out.println("线程运行中...");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("线程结束");
            }
        };
        //主循环中等待3秒
        t.start();
        Thread.sleep(3000);
        //三秒钟后,就把flag改为false
        flag = false;
    }
}
